# -*- coding:utf-8 -*-
"""
作者：谷台阳
日期：2022年10月28日
"""
import cv2
import numpy as np
import pandas as pd
from openpyxl import load_workbook
import time
from matplotlib import pyplot as plt

def sys_moments(img):
    """
    :param img:
    :return: humoment:
    """
    moments = cv2.moments(img)
    humoments = cv2.HuMoments(moments)
    humoment = (np.log(np.abs(humoments))) / np.log(10)
    return humoment

def main():
    h0 = []
    h1 = []
    h2 = []
    h3 = []
    h4 = []
    h5 = []
    h6 = []
    label = []
    wb = load_workbook("origin.xlsx")
    sheets = wb.worksheets
    start = time.perf_counter()
    data = pd.read_excel("clear.xlsx", sheet_name="clear", usecols=["Figure Name"])
    for i in range(0, 9950):
        num = int(data.loc[i].values)
        img = cv2.imread('diff/diff{}.jpg'.format(num), 0)
        sys_humoments = sys_moments(img)
        # print('Hu不变矩为：\n', sys_humoments)
        [[h00], [h10], [h20], [h30], [h40], [h50], [h60]] = sys_humoments
        h0.append(h00)
        h1.append(h10)
        h2.append(h20)
        h3.append(h30)
        h4.append(h40)
        h5.append(h50)
        h6.append(h60)
        # print(h0, h1, h2, h3, h4, h5, h6)
        lb = sheets[0].cell(i + 1, 2).value
        # print(lb)
        label.append(lb)

        last = time.perf_counter() - start
        c = int(i / 9950 * 100)
        a = "*" * c
        b = "-" * (100 - c)
        print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, last), end="")

    frame = pd.DataFrame({'H0': h0, 'H1': h1, 'H2': h2, 'H3': h3, 'H4': h4, 'H5': h5, 'H6': h6, 'label': label})
    frame.to_csv("Hu.csv", index=False, sep=',')
    x_axi = [i for i in range(0, 9950)]
    plt.title("Hu")
    plt.plot(x_axi, h0, color="red", label="H0")
    plt.plot(x_axi, h1, color="blue", label="H1")
    plt.plot(x_axi, h2, color="green", label="H2")
    plt.plot(x_axi, h3, color="pink", label="H3")
    plt.plot(x_axi, h4, color="purple", label="H4")
    plt.plot(x_axi, h5, color="yellow", label="H5")
    plt.plot(x_axi, h6, color="orange", label="H6")
    plt.legend()
    plt.show()

if __name__ == '__main__':
    main()
